<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Guestbook extends Controller
{
	var $site_title;
	var $floatim = '';
	
	function Guestbook()
	{
		parent::Controller();
		$this->load->database();
		$this->load->model('com_model');
		$this->load->library('form_validation');
		$this->site_title=$this->config->item('sys_site_title');
		if($this->config->item('sys_is_open_im') == '1')
		{
			$this->floatim = $this->com_model->floatim();
		}
	}
	
	function index()
	{
		$nav = '客户留言';
		$page=array(
			'page_base' => CTL_FOLDER.'guestbook/index',
			'offset'   => $this->uri->segment($this->com_model->uri_segnum+1,0),
 			'per_page' => 10,
			'sql'  => 'SELECT user_name,content,reply,reply_date,create_date FROM '.$this->db->dbprefix.'guestbook WHERE is_pass = 1 ORDER BY id DESC'
		);
		$query = $this->common_model->get_page_records($page);
		$data = array(
			'site_title'		=> $nav.'--'.$this->site_title,
			'guestbook'		=> $query['query'],
			'paginate'		=> $query['paginate'],
			'nav'		=> ' &gt; '.$nav
		);
		$this->load->view(TPL_FOLDER.'guestbook',$data);
	}
	
	function save_guestbook()
	{
		$this->form_validation->set_rules('user_name','联系人','trim|required');
		$this->form_validation->set_rules('email','电子邮箱','trim|valid_email');
		$this->form_validation->set_rules('content','留言内容','required');
		$this->form_validation->set_rules('validate_code','验证码','required');
		
		$tx_msg='';
		if($this->form_validation->run()==FALSE)
		{
			$tx_msg.=validation_errors();
		}
		if($tx_msg!='')
		{
			echo_msg($tx_msg);
		}
		session_start();
		if (strtoupper($this->input->post('validate_code')) !== $_SESSION['validate_code'])
		{
			echo_msg('<li>验证码有误</li>');
		}
		
		$data=array(
			'user_name'=>$this->input->post('user_name',TRUE),
			'tel'=>$this->input->post('tel',TRUE),
			'is_pass'=>0,
			'email'=>$this->input->post('email',TRUE),
			'content'=>$this->input->post('content',TRUE),
			'create_date'=>time()
		);
		$this->db->insert('guestbook',$data);
		
		$tx_msg='<li>留言提交成功，该留言需审核之后才能发布.</li>';
		echo_msg($tx_msg,'','yes');
	}
}
?>